import 'dart:io';

import 'package:dio/dio.dart';
class Error{
  int status;
  String message;
  Error(this.status, this.message);

  @override
  String toString() {
    return 'Error{status: $status, message: $message}';
  }
}
class Errorhandle{
  static const int success = 200;
  static const int unauthorized = 401;
  static const int forbidden = 403;
  static const int not_found = 404;
  static const int user_or_password_error= 426;

  static const int net_error = 1000;
  static const int parse_error = 1001;
  static const int socket_error = 1002;
  static const int http_error = 1003;
  static const int timeout_error = 1004;
  static const int unknown_error = 1011;

  static Error handleError(exception){
    if(exception is DioException){
      if(exception.type == DioExceptionType.receiveTimeout ||exception.type == DioExceptionType.sendTimeout ||exception.type == DioExceptionType.connectionTimeout){
        return Error(timeout_error,'连接超时！');
      } else if(exception.type == DioExceptionType.unknown ||exception.type == DioExceptionType.badResponse ||exception.type == DioExceptionType.connectionError){
        dynamic error = exception.error;
        if(error is SocketException){
          return Error(socket_error,'网络异常，请检查你的网络！');
        }
        if(error is HttpException){
          return Error(socket_error,'服务器异常！');
        }
        return Error(net_error,'网络异常，请检查你的网络！');
      }else{
        return Error(unknown_error,'网络异常，请检查你的网络！');
      }
    }else{
      return Error(unknown_error,'网络异常，请检查你的网络！');
    }
  }


}